package com.jtech.together.view.fragment;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jtech.together.R;
import com.jtech.together.model.TrendsModel;
import com.jtech.together.model.UserModel;
import com.jtech.together.model.event.AddTrendsEvent;
import com.jtech.together.model.event.TrendsEvent;
import com.jtech.together.mvp.contract.TrendsContract;
import com.jtech.together.mvp.presenter.TrendsPresenter;
import com.jtech.together.util.ActivityJump;
import com.jtech.together.util.Bus;
import com.jtech.together.util.ImageGallery;
import com.jtech.together.util.LocationSelect;
import com.jtech.together.view.activity.TrendsDetailActivity;
import com.jtech.together.view.activity.UserProfileActivity;
import com.jtech.together.view.adapter.TrendsAdapter;
import com.jtech.together.view.fragment.base.BaseFragment;
import com.jtech.together.view.weight.dialog.CustomProgressDialog;
import com.jtechlib2.listener.OnItemClickListener;
import com.jtechlib2.listener.OnLoadListener;
import com.jtechlib2.view.JRecyclerView;
import com.jtechlib2.view.RecyclerHolder;
import com.jtechlib2.view.RefreshLayout;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import cn.bmob.v3.datatype.BmobGeoPoint;

/**
 * 动态列表
 * Created by wuxubaiyang on 2017/5/10.
 */
public class TrendsFragment extends BaseFragment implements TrendsContract.View, RefreshLayout.OnRefreshListener, OnLoadListener,
        TrendsAdapter.OnTrendsListener, OnItemClickListener {
    @Bind(R.id.refreshlayout_trends)
    RefreshLayout refreshLayout;
    @Bind(R.id.jrecyclerview_trends)
    JRecyclerView jRecyclerView;

    private TrendsContract.Presenter presenter;
    private TrendsAdapter trendsAdapter;

    @Override
    public View createView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
        return layoutInflater.inflate(R.layout.fragment_trends, viewGroup, false);
    }

    @Override
    protected void initVariables(Bundle bundle) {
        //上车
        Bus.getOn(this);
        //绑定P类
        presenter = new TrendsPresenter(getActivity(), this, bundle);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        //设置layoutmanager
        jRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //设置支持加载更多
        jRecyclerView.setLoadMore(true);
        //设置适配器
        trendsAdapter = new TrendsAdapter(getActivity());
        jRecyclerView.setAdapter(trendsAdapter);
        //设置分页从0开始
        trendsAdapter.setStartPage(0);
        //设置下拉刷新监听
        refreshLayout.setOnRefreshListener(this);
        //设置加载更多监听
        jRecyclerView.setOnLoadListener(this);
        //设置item事件监听
        trendsAdapter.setOnTrendsListener(this);
        //设置列表点击事件
        jRecyclerView.setOnItemClickListener(this);
    }

    @Override
    protected void loadData() {
        refreshLayout.startRefreshing();
    }

    public static TrendsFragment newInstance() {
        Bundle args = new Bundle();
        TrendsFragment fragment = new TrendsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 添加动态事件
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void addTrendsEvent(AddTrendsEvent event) {
        refreshLayout.startRefreshing();
    }

    /**
     * 动态事件变化监听
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void TrendsChange(TrendsEvent event) {
        String trendsId = event.getTrendsId();
        int count = event.getCount();
        switch (event.getState()) {
            case TrendsEvent.STATE_LIKE://喜欢
                trendsAdapter.updateItemLike(trendsId, count);
                break;
            case TrendsEvent.STATE_UNLIKE://踩
                trendsAdapter.updateItemUnlike(trendsId, count);
                break;
            case TrendsEvent.STATE_COMMENT://评论
                trendsAdapter.updateItemComment(trendsId, count);
                break;
        }
    }

    @Override
    public void loadSuccess(List<TrendsModel> trendsModels, boolean loadMore) {
        refreshLayout.refreshingComplete();
        if (trendsModels.size() > 0) {
            trendsAdapter.setDatas(trendsModels, loadMore);
            jRecyclerView.setLoadCompleteState();
        } else {
            jRecyclerView.setLoadFinishState();
        }
    }

    @Override
    public void loadFail(String error) {
        refreshLayout.refreshingComplete();
        jRecyclerView.setLoadFailState();
        Snackbar.make(jRecyclerView, error, Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void operationSuccess(TrendsModel trendsModel) {
        CustomProgressDialog.dismissProgressDialog();
    }

    @Override
    public void operationFail(String error) {
        CustomProgressDialog.dismissProgressDialog();
        Snackbar.make(refreshLayout, error, Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void onRefresh() {
        presenter.loadTrends(trendsAdapter.getPage(false), trendsAdapter.getDisplayNumber(), false);
    }

    @Override
    public void loadMore() {
        presenter.loadTrends(trendsAdapter.getPage(true), trendsAdapter.getDisplayNumber(), true);
    }

    @Override
    public void onAvatarClick(String userObjectId) {
        Bundle bundle = new Bundle();
        bundle.putString(UserProfileActivity.KEY_USER_OBJECT_ID, userObjectId);
        ActivityJump.build(getActivity(), UserProfileActivity.class)
                .addBundle(bundle)
                .jump();
    }

    @Override
    public void onUpClick(TrendsModel trendsModel, boolean isUp) {
        CustomProgressDialog.showProgressDialog(getActivity(), "正在操作");
        presenter.like(trendsModel, isUp ? -1 : 1);
    }

    @Override
    public void onDownClick(TrendsModel trendsModel, boolean isDown) {
        CustomProgressDialog.showProgressDialog(getActivity(), "正在操作");
        presenter.unlike(trendsModel, isDown ? -1 : 1);
    }

    @Override
    public void onCommentClick(TrendsModel trendsModel) {
        Bundle bundle = new Bundle();
        bundle.putString(TrendsDetailActivity.KEY_TRENDS_ID, trendsModel.getObjectId());
        ActivityJump.build(getActivity(), TrendsDetailActivity.class)
                .addBundle(bundle)
                .jump();
    }

    @Override
    public void onLocationClick(BmobGeoPoint location, UserModel userModel, String describe) {
        LocationSelect.showTarget(getActivity(), new double[]{location.getLatitude(), location.getLongitude()},
                userModel.getNickname() + "\t在这里!", describe);
    }

    @Override
    public void onImageClick(List<String> images, int index) {
        ImageGallery.gallery(getActivity(), new ArrayList<>(images), index);
    }

    @Override
    public void onItemClick(RecyclerHolder holder, View view, int position) {
        TrendsModel trendsModel = trendsAdapter.getItem(holder.getAdapterPosition());
        Bundle bundle = new Bundle();
        bundle.putString(TrendsDetailActivity.KEY_TRENDS_ID, trendsModel.getObjectId());
        ActivityJump.build(getActivity(), TrendsDetailActivity.class)
                .addBundle(bundle)
                .jump();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //下车
        Bus.getOff(this);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(TAG);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(TAG);
    }
}